package com.example.demo.test.alc;

import com.example.demo.socket.WebSocketServer;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.stereotype.Component;

import java.util.Random;

import static com.example.demo.test.selenium.SeleniumHelper.*;
@Component
public class MyWallet implements BaseTest{
    public void test() throws Exception {
        WebSocketServer.screenInfoMsg("======================myWalletTest======================");

        WebSocketServer.screenInfoMsg("打开 MyWalletList页面");
        waitAndClickByPath("//a[@href='/us/en/mywallet']");

        WebSocketServer.screenInfoMsg("点击 addNewCard ");
        waitAndClickByPath("//*[@id='mywalletContent']/div/div[2]/button");

        WebSocketServer.screenInfoMsg("等待 cardEdit 页面加载完成");
        WebElement element = driver.findElement(By.xpath("//*[@id='addmywallet']"));
        driver.switchTo().frame(element);
        waitByPath("//*[@id='c-ct']");
        submitMyWallet();
    }

    public void submitMyWallet() {
        int i = new Random().nextInt();
        String cardName = "myCard" + i;
        WebSocketServer.screenInfoMsg("生成 MyWallet表单");


        findByIdAndSelectByIndex("c-ct", 0);
        findByIdAndClearSend("c-cardnumber", "371449635398431");
        findByIdAndSelectByIndex("c-exmth", 5);
        findByIdAndSelectByIndex("c-exyr", 3);
        findByIdAndClearSend("c-cvv", "1234");
        driver.switchTo().defaultContent();
        findByIdAndClearSend("card_edit_Ccowner", cardName);
        findByIdAndClearSend("card_edit_NickName", cardName);

        findByIdAndClearSend("addrss_FirstName", "xw");
        findByIdAndClearSend("addrss_LastName", "zhang");
        findByIdAndClearSend("addrss_StreetName", "houchangcun");
        findByIdAndClearSend("addrss_StreetNumber", "ruanjianyuan");
        findByIdAndClearSend("addrss_Town", "beijing");
        findByIdAndSelectByIndex("addrss_Region", 2);
        findByIdAndClearSend("addrss_PostalCode", "10000");
        findByIdAndClearSend("addrss_Company", "lenovo");
        findByIdAndClearSend("addrss_Phone1", "15811102023");
        findByIdAndClearSend("addrss_ExtPhone1", "110");
        findByIdAndClearSend("addrss_Phone2", "15811102023");
        findByIdAndClearSend("addrss_ExtPhone2", "119");
        findByIdAndClearSend("addrss_Email", "978880162@qq.com");


        WebSocketServer.screenInfoMsg("准备提交 MyWallet表单");
        driver.findElement(By.xpath("//*[@id='address']/div[2]/button[2]")).click();
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(cardName)));
            WebSocketServer.screenInfoMsg("MyWallet 保存成功");
        } catch (Exception e) {
            WebSocketServer.screenInfoMsg("MyWallet 保存失败 cardName 与预期不一致 期望:%s 实际:%s", cardName, "未找到");
        }
    }
}
